/*
'  Tidy Modules - Current Date Extended Skin Object
'  Copyright (c) 2010
'  by Gilles Le Pigocher ( glepigocher@yaho.fr )
' 
'  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
'  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
'  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
'  to permit persons to whom the Software is furnished to do so, subject to the following conditions:
' 
'  The above copyright notice and this permission notice shall be included in all copies or substantial portions 
'  of the Software.
' 
'  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
'  TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
'  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
'  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
'  DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Globalization;
using DotNetNuke.Entities.Users;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Skins;

namespace TidyModules.CurrentDateExtended
{
    public partial class CurrentDateExt : SkinObjectBase
    {
        #region Private Members

        private CalendarNames _calendarName = CalendarNames.Undefined;
        private string _cssClass;
        private string _cultureName;
        private string _dateFormat;
        private GregorianCalendarTypes _gregorianType = GregorianCalendarTypes.Localized;
        private int _hijriAdjustment = 0;

        #endregion

        #region Public Properties

        public enum CalendarNames
        {
            Gregorian,
            Hebrew,
            Hijri,
            Japanese,
            Korean,
            Persian,
            Taiwan,
            ThaiBuddhist,
            UmAlQura,
            Undefined
        }

        public CalendarNames CalendarName
        {
            set { _calendarName = value; }
        }

        public string CssClass
        {
            set { _cssClass = value; }
        }

        public string CultureName
        {
            set { _cultureName = value; }
        }

        public string DateFormat
        {
            set { _dateFormat = value; }
        }

        public GregorianCalendarTypes GregorianType
        {
            set { _gregorianType = value; }
        }

        public int HijriAdjustment
        {
            set { _hijriAdjustment = value; }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            string dateText = "";
            try
            {
                UserTime userTime = new UserTime();
                CultureInfo ci;
                if (string.IsNullOrEmpty(_cultureName) == false)
                    ci = new CultureInfo(_cultureName);
                else
                    ci = new CultureInfo(CultureInfo.CurrentCulture.Name);
                if (_calendarName != CalendarNames.Undefined)
                    ci.DateTimeFormat.Calendar = GetCalendar(_calendarName);
                if (string.IsNullOrEmpty(_dateFormat) == true)
                    dateText = userTime.CurrentUserTime.ToString(ci);
                else
                    dateText = userTime.CurrentUserTime.ToString(_dateFormat, ci);
            }
            catch (Exception ex)
            {
                string _resource = this.TemplateSourceDirectory + "/App_LocalResources/CurrentDateExt.ascx.resx";
                dateText = Localization.GetString("Error", _resource);
                Exceptions.LogException(ex);
            }
            finally
            {
                if (string.IsNullOrEmpty(_cssClass) == false)
                    lblDate.CssClass = _cssClass;
                lblDate.Text = dateText;
            }
        }

        #endregion

        #region Private Methods

        private Calendar GetCalendar(CalendarNames name)
        {
            switch (name)
            {
                case CalendarNames.Gregorian:
                    return new GregorianCalendar(_gregorianType);
                case CalendarNames.Hebrew:
                    return new HebrewCalendar();
                case CalendarNames.Hijri:
                    HijriCalendar hc = new HijriCalendar();
                    hc.HijriAdjustment = _hijriAdjustment;
                    return hc;
                case CalendarNames.Japanese:
                    return new JapaneseCalendar();
                case CalendarNames.Korean:
                    return new KoreanCalendar();
                case CalendarNames.Persian:
                    return new PersianCalendar();
                case CalendarNames.Taiwan:
                    return new TaiwanCalendar();
                case CalendarNames.ThaiBuddhist:
                    return new ThaiBuddhistCalendar();
                case CalendarNames.UmAlQura:
                    return new UmAlQuraCalendar();
                default:
                    return null;
            }
        }

        #endregion
    }
}